/*
 * Créer un programme qui prend en argument un nom de
 * répertoire et édite ses fichiers, leur nature et les droits
 * associés.
 */
package listdir;

import java.io.File;

/**
 *
 * @author pfares
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int MAXFILES = 100; // maximun files per directory
        if (args.length < 1) {
            System.out.println("Usage: Java listDir dirName");
            return;
        }
        File dirName = new File(args[0]);
        if (!dirName.exists()) {
            System.out.println(args[0] + " does not exist on file system");
            return;
        }
        if (!dirName.isDirectory()) {
            System.out.println(args[0] + " is not a directory");
            return;
        }
        System.out.print("Files in directory: " + dirName.getAbsolutePath() + "\n\n\n");
        String fileArr[] = new String[MAXFILES];
        try {
            fileArr = dirName.list();
        } catch (Exception e) {
            System.out.println("Error listing contents of " + dirName);
            System.exit(0);
        }
        for (int i = 0; i < fileArr.length; i++) {
            File filename = new File(args[0] + System.getProperty("file.separator") + fileArr[i]);
            System.out.println(" Filename: " + fileArr[i]);
            if (filename.exists()) {
                if (filename.isFile()) {
                    System.out.println(" Is a file");
                }
                if (filename.isDirectory()) {
                    System.out.println(" Is a directory");
                }
                if (filename.canWrite()) {
                    System.out.println(" You have write permission");
                }
                if (filename.canRead()) {
                    System.out.println(" You have read permission");
                }
            } else {
                System.out.println(" Does not exist");
            }
            System.out.println("");
        }

    }
}
